package info.androidhive.slidingmenu;

//import com.example.httpdownloadtext.R;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;



public class HomeActivity extends ListActivity {
	private TextView title, idEli, detail, icon;
	private int index = 0;
	
	
    

  //Add   
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_listview);
        
        GetXMLTask task = new GetXMLTask();
        task.execute(new String[] { "http://tumnaja-pc:8081/server-citcom_km/connect_DB.php" });
        
        
        
        
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
       
        CitcomData.showDdataSingle = ((ArrayList<DataServer>) CitcomData.mainDataServet).get(position);
        viewDetail();
    }
    
    
    
    
    private class GetXMLTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String output = null;
            for (String url : urls) {
                output = getOutputFromUrl(url);
            }
            return output;
        }
 
        private String getOutputFromUrl(String url) {
            String output = null;
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        		
         
        		httpPost.setEntity(new UrlEncodedFormEntity(urlParameters));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                output = EntityUtils.toString(httpEntity);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e){
                e.printStackTrace();
            }
            return output;
        }
 
        @Override
        protected void onPostExecute(String jsonString) {
        	System.out.println(jsonString);
        	List<DataServer> DataServerlList = new ArrayList<DataServer>();
        	DataServer DataServer = null;
        	JSONArray jArray;
			try {
				jArray = new JSONArray(jsonString);
				for (int i = 0; i < jArray.length(); i++) {
					DataServer = new DataServer();
	                JSONObject json_data = jArray.getJSONObject(i);
	                DataServer.setIcon(json_data.getString("icon"));
	                DataServer.setTitle(json_data.getString("title"));
	                DataServer.setDetail(json_data.getString("detail"));
	                DataServer.setId(json_data.getString("id"));
	               
	                DataServerlList.add(DataServer);
	            }
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			CitcomData.mainDataServet = DataServerlList;
			showData();
        }
    }
    public void showData(){
         List<DataServer> mainStrList = (List<DataServer>) CitcomData.mainDataServet;
         String[] datalist = new String[mainStrList.size()];
         for (int i = 0; i < mainStrList.size(); i++) {
         	datalist[i] = mainStrList.get(i).getId()+" "+mainStrList.get(i).getTitle();
         	if(datalist[i].length()>=30){
         		datalist[i] = datalist[i].substring(0,30)+"...";
         	}
 		}
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, datalist);
         setListAdapter(adapter);
    }
    
    public void viewDetail(){
		Intent i = new Intent(this, ViewDetail.class);
        startActivity(i);
	}
}
